<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="组织名称" prop="deptNm">
      <el-input v-model="dataForm.deptNm" placeholder="请输入组织名称"></el-input>
    </el-form-item>

    <el-form-item label="是否物业" prop="isEstate">
      <el-select v-model="dataForm.isEstate" placeholder="请选择" @change="selectEstate">
        <el-option
          v-for="item in isEstate"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>

      <el-form-item label="选择小区">
        <el-select v-model="dataForm.areaId" placeholder="请选择小区" :disabled="areaShow"  @change="selectArea">
          <el-option
            v-for="item in areaValue"
            :key="item.areaId"
            :label="item.areaName"
            :value="item.areaId">
          </el-option>
        </el-select>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        areaShow: true,
        visible: false,
        areaValue: [],
        isEstate: [{
          value: '0',
          label: '不是物业'
        }, {
          value: '1',
          label: '是物业'
        }],
        dataForm: {
          deptId: 0,
          deptNm: '',
          isEstate: '',
          areaId: '',
          areaName: ''
        },
        dataRule: {
          deptNm: [
            { required: true, message: '组织名称不能为空', trigger: 'blur' }
          ],
          isEstate: [
            { required: true, message: '请选择是否物业', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.deptId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.$http({
            url: this.$http.adornUrl('/area/area/forList'),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log(data.list)
              this.areaValue = data.list
            }
          })
          if (this.dataForm.deptId) {
            this.$http({
              url: this.$http.adornUrl(`/dept/dept/info/${this.dataForm.deptId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deptNm = data.dept.deptNm
                this.dataForm.isDel = data.dept.isDel
                this.dataForm.createTime = data.dept.createTime
                this.dataForm.updateTime = data.dept.updateTime
                this.dataForm.tenantId = data.dept.tenantId
                this.dataForm.isEstate = data.dept.isEstate
                this.dataForm.areaId = data.dept.areaId
                this.dataForm.areaName = data.dept.areaName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.isEstate === '1' && !this.dataForm.areaId) {
              this.$message.error('请选择管理区域')
            } else {
              this.$http({
                url: this.$http.adornUrl(`/dept/dept/${!this.dataForm.deptId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  'deptId': this.dataForm.deptId || undefined,
                  'deptNm': this.dataForm.deptNm,
                  'isEstate': this.dataForm.isEstate,
                  'areaId': this.dataForm.areaId,
                  'areaName': this.dataForm.areaName
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          }
        })
      },
      selectEstate (d) {
        if (d === '1') {
          this.areaShow = false
        } else {
          this.areaShow = true
          this.dataForm.areaName = ''
          this.dataForm.areaId = ''
        }
      },
      selectArea (areaId) {
        let obj = {}
        obj = this.areaValue.find((item) => {
          return item.areaId === areaId
        })
        console.log(obj.areaName)
        this.dataForm.areaName = obj.areaName
      }
    }
  }
</script>
